# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
.PHONY: presto_protocol presto_protocol-json presto_protocol-cpp

default: presto_protocol

presto_protocol: presto_protocol-cpp

presto_protocol-cpp: presto_protocol-json

	# build core structs
	echo "// DO NOT EDIT : This file is generated by chevron" > core/presto_protocol_core.cpp
	chevron -d core/presto_protocol_core.json core/presto_protocol-json-cpp.mustache >> core/presto_protocol_core.cpp
	echo "// DO NOT EDIT : This file is generated by chevron" > core/presto_protocol_core.h
	chevron -d core/presto_protocol_core.json core/presto_protocol-json-hpp.mustache >> core/presto_protocol_core.h
	clang-format -style=file -i core/presto_protocol_core.h core/presto_protocol_core.cpp

	# build hive connector related structs
	echo "// DO NOT EDIT : This file is generated by chevron" > connector/hive/presto_protocol_hive.cpp
	chevron -d connector/hive/presto_protocol_hive.json connector/hive/presto_protocol-json-cpp.mustache >> connector/hive/presto_protocol_hive.cpp
	echo "// DO NOT EDIT : This file is generated by chevron" > connector/hive/presto_protocol_hive.h
	chevron -d connector/hive/presto_protocol_hive.json connector/hive/presto_protocol-json-hpp.mustache >> connector/hive/presto_protocol_hive.h
	clang-format -style=file -i connector/hive/presto_protocol_hive.h connector/hive/presto_protocol_hive.cpp

	# build iceberg connector related structs
	echo "// DO NOT EDIT : This file is generated by chevron" > connector/iceberg/presto_protocol_iceberg.cpp
	chevron -d connector/iceberg/presto_protocol_iceberg.json connector/iceberg/presto_protocol-json-cpp.mustache >> connector/iceberg/presto_protocol_iceberg.cpp
	echo "// DO NOT EDIT : This file is generated by chevron" > connector/iceberg/presto_protocol_iceberg.h
	chevron -d connector/iceberg/presto_protocol_iceberg.json connector/iceberg/presto_protocol-json-hpp.mustache >> connector/iceberg/presto_protocol_iceberg.h
	clang-format -style=file -i connector/iceberg/presto_protocol_iceberg.h connector/iceberg/presto_protocol_iceberg.cpp

	# build tpch connector related structs
	echo "// DO NOT EDIT : This file is generated by chevron" > connector/tpch/presto_protocol_tpch.cpp
	chevron -d  connector/tpch/presto_protocol_tpch.json connector/tpch/presto_protocol-json-cpp.mustache >> connector/tpch/presto_protocol_tpch.cpp
	echo "// DO NOT EDIT : This file is generated by chevron" > connector/tpch/presto_protocol_tpch.h
	chevron -d  connector/tpch/presto_protocol_tpch.json connector/tpch/presto_protocol-json-hpp.mustache >> connector/tpch/presto_protocol_tpch.h
	clang-format -style=file -i connector/tpch/presto_protocol_tpch.h connector/tpch/presto_protocol_tpch.cpp

	# build arrow_flight connector related structs
	echo "// DO NOT EDIT : This file is generated by chevron" > connector/arrow_flight/presto_protocol_arrow_flight.cpp
	chevron -d  connector/arrow_flight/presto_protocol_arrow_flight.json connector/arrow_flight/presto_protocol-json-cpp.mustache >> connector/arrow_flight/presto_protocol_arrow_flight.cpp
	echo "// DO NOT EDIT : This file is generated by chevron" > connector/arrow_flight/presto_protocol_arrow_flight.h
	chevron -d  connector/arrow_flight/presto_protocol_arrow_flight.json connector/arrow_flight/presto_protocol-json-hpp.mustache >> connector/arrow_flight/presto_protocol_arrow_flight.h
	clang-format -style=file -i connector/arrow_flight/presto_protocol_arrow_flight.h connector/arrow_flight/presto_protocol_arrow_flight.cpp

presto_protocol-json: 
	./java-to-struct-json.py --config core/presto_protocol_core.yml core/special/*.java core/special/*.inc -j | jq . > core/presto_protocol_core.json
	./java-to-struct-json.py --config connector/hive/presto_protocol_hive.yml connector/hive/special/*.inc -j | jq . > connector/hive/presto_protocol_hive.json
	./java-to-struct-json.py --config connector/iceberg/presto_protocol_iceberg.yml connector/iceberg/special/*.inc -j | jq . > connector/iceberg/presto_protocol_iceberg.json
	./java-to-struct-json.py --config connector/tpch/presto_protocol_tpch.yml connector/tpch/special/*.inc -j | jq . > connector/tpch/presto_protocol_tpch.json
	./java-to-struct-json.py --config connector/arrow_flight/presto_protocol_arrow_flight.yml connector/arrow_flight/special/*.inc -j | jq . > connector/arrow_flight/presto_protocol_arrow_flight.json

presto_protocol.proto: presto_protocol-json
	pystache presto_protocol-protobuf.mustache core/presto_protocol_core.json > core/presto_protocol_core.proto
	pystache presto_protocol-protobuf.mustache connector/hive/presto_protocol_hive.json > connector/hive/presto_protocol_hive.proto
	pystache presto_protocol-protobuf.mustache connector/iceberg/presto_protocol_iceberg.json > connector/iceberg/presto_protocol_iceberg.proto
	pystache presto_protocol-protobuf.mustache connector/tpch/presto_protocol_tpch.json > connector/tpch/presto_protocol_tpch.proto
	pystache presto_protocol-protobuf.mustache connector/arrow_flight/presto_protocol_arrow_flight.json > connector/arrow_flight/presto_protocol_arrow_flight.proto
